package com.xy.biz.ts.service.impl;

import com.xy.biz.ts.domain.bo.TrainBO;
import com.xy.biz.ts.domain.bo.TrainSumBO;
import com.xy.biz.ts.manager.TrainFlowHandleManager;
import com.xy.tool.context.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.xy.biz.ts.domain.bo.TrainFlowConstants.*;

@Slf4j
@Service
public class TrainFlowServiceSupport {

    @Autowired
    private TrainFlowHandleManager trainFlowHandleManager;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;


    public Task startFirstTask(TrainBO trainBO, String processDefinitionKey, Long assignee) {
        Map<String, Object> variables = new HashMap<>();
        variables.put(VARIABLE_ASSIGNEE, assignee);
        variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_ACCEPT));
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,
                trainBO.getTrainId().toString(), variables);
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getProcessInstanceId())
                .singleResult();
        taskService.complete(task.getId());
        return task;
    }

    public Task startFirstTask(TrainSumBO trainSumBO, String processDefinitionKey, Long assignee) {
        Map<String, Object> variables = new HashMap<>();
        variables.put(VARIABLE_ASSIGNEE, assignee);
        variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_ACCEPT));
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,
                trainSumBO.getTrainSumId().toString(), variables);
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getProcessInstanceId())
                .singleResult();
        taskService.complete(task.getId());
        return task;
    }

    /**
     * 终止
     */
    public void finish(TrainBO trainBO) {
        Task task = getCurrentTask(trainBO);
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_END));
            taskService.complete(task.getId(), variables);
        } else {
            log.error("培训流程" + trainBO.getTrainId() + "没有任务");
        }
    }

    /**
     * 终止
     */
    public void finish(TrainSumBO trainSumBO) {
        Task task = getCurrentTask(trainSumBO);
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_END));
            taskService.complete(task.getId(), variables);
        } else {
            log.error("培训流程" + trainSumBO.getTrainSumId() + "没有任务");
        }
    }

    public Task finishLastTask(TrainBO trainBO) {
        Task task = getCurrentTask(trainBO);
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_ACCEPT));
            taskService.complete(task.getId(), variables);
        } else {
            log.error("培训流程" + trainBO.getTrainId() + "没有任务");
        }
        return task;
    }

    public Task finishLastTask(TrainSumBO trainSumBO) {
        Task task = getCurrentTask(trainSumBO);
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_HANDLE, String.valueOf(HANDLE_RESULT_ACCEPT));
            taskService.complete(task.getId(), variables);
        } else {
            log.error("培训流程" + trainSumBO.getTrainSumId() + "没有任务");
        }
        return task;
    }


    public Task getCurrentTask(TrainBO trainBO) {
        return taskService.createTaskQuery()
                .taskAssignee(UserContextHolder.getUserId().toString())
                .processInstanceId(trainBO.getForeignKey())
                .singleResult();
    }

    public Task getCurrentTask(TrainSumBO trainSumBO) {
        return taskService.createTaskQuery()
                .taskAssignee(UserContextHolder.getUserId().toString())
                .processInstanceId(trainSumBO.getForeignKey())
                .singleResult();
    }

    public void gotoNextTask(TrainBO trainBO, Long assignee, int result) {
        Task task = getCurrentTask(trainBO);
        if (task != null) {
            gotoNextTask(task, assignee, result);
        } else {
            log.error("培训流程" + trainBO.getTrainId() + "没有任务");
        }
    }

    public void gotoNextTask(TrainSumBO trainSumBO, Long assignee, int result) {
        Task task = getCurrentTask(trainSumBO);
        if (task != null) {
            gotoNextTask(task, assignee, result);
        } else {
            log.error("培训流程" + trainSumBO.getTrainSumId() + "没有任务");
        }
    }

    public void gotoNextTask(Task task, Long assignee, int result) {
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_ASSIGNEE, assignee);
            variables.put(VARIABLE_HANDLE, String.valueOf(result));

            taskService.complete(task.getId(), variables);
        } else {
            log.error("培训流程没有任务");
        }
    }

    public void gotoNextTask2(TrainBO trainBO, List<Long> assigneeList, int result) {
        Task task = getCurrentTask(trainBO);
        if (task != null) {
            gotoNextTask2(task, assigneeList, result);
        } else {
            log.error("培训流程" + trainBO.getTrainId() + "没有任务");
        }
    }

    public void gotoNextTask2(Task task, List<Long> assigneeList, int result) {
        if (task != null) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(VARIABLE_ASSIGNEE_LIST, assigneeList);
            variables.put(VARIABLE_HANDLE, String.valueOf(result));

            taskService.complete(task.getId(), variables);
        } else {
            log.error("文档流程没有任务");
        }
    }
}
